﻿
using System.Collections.Generic;

namespace Winservice.demo.Queue
{
    /// <summary>
    /// 队列管理
    /// </summary>
    public class QueueManager
    {
        /// <summary>
        /// 队列管理-构造函数
        /// </summary>
        public QueueManager() { }

        //定义队列集合 
        private readonly Queue<QueueTask> queueList = new Queue<QueueTask>();

        /// <summary>
        /// 添加队列具体信息
        /// </summary>
        /// <param name="QueueTask">task</param>
        public void AddQueue(QueueTask task)
        {
            //TaskApproval_Business business = new TaskApproval_Business();
            lock (this)
            {
                //try
                //{
                //从队列一端插入内容 
                queueList.Enqueue(task);
                //进入数据库，状态为【开始进入队列中】
                //business.UpdateTaskApprovalStatusAsync(task.TaskApprovalID, TASK_STATUS_Enum.QueueIn, string.Empty);
                //}
                //catch (Exception ce)
                //{
                //    //business.UpdateTaskApprovalStatusAsync(task.TaskApprovalID, TASK_STATUS_Enum.Exception, "入队列异常:" + ce.Message);
                //}
                //Console.WriteLine("成功插入文档:" + mailSendInfo.title);
            }
        }

        /// <summary>
        /// 读取队列信息
        /// </summary>
        /// <returns></returns>
        public QueueTask GetQueueItem()
        {
            QueueTask task = null;
            lock (this)
            {
                task = queueList.Dequeue();
                return task;
            }
        }

        /// <summary>
        /// 只读属性,确定队列中是不是还有元素
        /// </summary>
        public bool IsAvailable
        {
            get
            {
                return queueList.Count > 0;
            }
        }


    }
}
